package com.refuel.controller;

import com.refuel.domain.Goods;
import com.refuel.domain.GoodsItem;
import com.refuel.enumeration.GoodsItemType;
import com.refuel.response.BaseResponse;
import com.refuel.response.PageResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * @author zhanghao
 * @description 商品配置器
 * @date 2024/5/9 16:08
 **/
@RestController
@RequestMapping("/goods")
@Tag(name = "商品控制器", description = "商品相关接口")
public class GoodsController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(GoodsController.class);

    /**
     * 首页顶部轮播图获取接口
     * @return BaseResponse
     */
    @Operation(summary = "首页顶部轮播图获取接口")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "成功"),
            @ApiResponse(responseCode = "400", description = "xx参数错误")
    })
    @RequestMapping(method = RequestMethod.GET, path = "/head/image/list")
    public BaseResponse<List<String>> headImageList() {
        BaseResponse<List<String>> response = new BaseResponse<>();

        List<String> headImageList = Arrays.asList("https://img.refuel.com/123456.jpg",
                "https://img.refuel.com/123456.jpg", "https://img.refuel.com/123456.jpg");
        response.setData(headImageList);
        return response;
    }

    /**
     * 首页顶部下单说明配置获取
     * @return BaseResponse
     */
    @Operation(summary = "首页顶部下单说明配置获取接口")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "成功"),
            @ApiResponse(responseCode = "400", description = "xx参数错误")
    })
    @RequestMapping(method = RequestMethod.GET, path = "/guide/fetch")
    public BaseResponse<String> guideGet() {

        String guide = "好礼1：下单的商品，可立即核销使用；\n" +
                "好礼2：即时抽奖，下单后可得到一次抽奖机会，抽取小礼品；\n" +
                "好礼3：积分抽奖，下单后可获得积分，参加积分抽奖活动，礼品\n" +
                "              丰厚。";
        return new BaseResponse<>(guide);
    }

    /**
     * 单个商品信息详情
     * @return BaseResponse
     */
    @Operation(summary = "单个商品信息详情接口")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "成功"),
            @ApiResponse(responseCode = "400", description = "xx参数错误")
    })
    @RequestMapping(method = RequestMethod.GET, path = "/detail")
    public BaseResponse<Goods> goodsDetail(@RequestParam(name = "goodsId") String goodsId) {
        logger.info("goodsConfig guideRequest: {}", goodsId);
        BaseResponse<Goods> response = new BaseResponse<>();

        Goods goods = buildGoods(201.00, "100元");
        response.setData(goods);
        return response;
    }

    /**
     * 商品列表
     * @return PageResponse
     */
    @Operation(summary = "商品列表接口")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "成功"),
            @ApiResponse(responseCode = "400", description = "xx参数错误")
    })
    @RequestMapping(method = RequestMethod.GET, path = "/list")
    public PageResponse<Goods> goodsList(@RequestParam(name = "goodsId") String goodsId,
                                         @RequestParam(name = "goodsName") String goodsName,
                                         @RequestParam(name = "pageNo") Integer pageNo,
                                         @RequestParam(name = "pageSize") Integer pageSize) {
        logger.info("goodsConfig goodsId: {}, goodsName: {}, pageNo: {}, pageSize: {}",
                goodsId, goodsName, pageNo, pageSize);
        PageResponse<Goods> response = new PageResponse<>();

        response.addData(buildGoods(201.00, "200元"));
        response.addData(buildGoods(301.00, "200元"));
        response.addData(buildGoods(401.00, "200元"));
        response.addData(buildGoods(501.00, "200元"));
        return response;
    }

    private Goods buildGoods(Double prices, String refuelCard) {
        Goods goods = new Goods();
        goods.setId("1111111");
        goods.setName("加油卡套餐");
        goods.setTitle("挡风玻璃车窗后视镜玻璃水去污水清洁剂");
        goods.setImgLink("https://img.refuel.com/123456.jpg");
        goods.setDescription("1、套餐价格：201元，包含200元加油卡，200积分以及一份小礼品;\n" +
                "2、积分可用于参与活动抽奖，活动多多，奖品多多；\n" +
                "3、下单立得一份小礼品，礼品系统随机赠送；\n" +
                "4、加油卡可在就近中石油、中石化加油站核销；\n" +
                "5、商品可在【我的】->【我的礼券】进行核销；\n" +
                "6、下单后注意商品核销终止时间，请及时核销。");
        goods.setPrice(prices);
        goods.setStock(100);

        List<GoodsItem> itemList = goods.getItemList();
        GoodsItem item = new GoodsItem();
        item.setName("加油卡");
        item.setType(GoodsItemType.REFUEL_CARD);
        item.setContent(refuelCard);
        itemList.add(item);
        item = new GoodsItem();
        item.setName("积分");
        item.setType(GoodsItemType.INTEGRAL);
        item.setContent("200元");
        itemList.add(item);
        item = new GoodsItem();
        item.setName("玩偶礼品");
        item.setType(GoodsItemType.GIFT);
        item.setContent("1个");
        itemList.add(item);
        goods.setItemList(itemList);

        return goods;
    }

}
